//
//  NightViewController.m
//  TTMusic
//
//  Created by 马金晖 on 16/4/7.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "NightViewController.h"

#define width     self.view.bounds.size.width
#define height    self.view.bounds.size.height


@interface NightViewController ()

@end

@implementation NightViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    imageView.image = [UIImage imageNamed:@"beijingNight.jpg"];
    
    [self.view addSubview:imageView];
    
    NSString *states = [[NSUserDefaults standardUserDefaults] objectForKey:@"night"];
    
    UISwitch *NightSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(width * 0.45, height * 0.45, width * 0.1, width * 0.1)];
    if ([states isEqualToString:@"yes"]) {
        NightSwitch.on=YES;
    }
    
    else{
        NightSwitch.on=NO;
    }
    [NightSwitch addTarget:self action:@selector(NightSwitchAction:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:NightSwitch];
    
    UILabel *NightLabel = [[UILabel alloc]initWithFrame:CGRectMake(width * 0.2, height * 0.3, width * 0.6, height * 0.15)];
    NightLabel.text = @"夜间模式开关";
    NightLabel.font = [UIFont systemFontOfSize:26];
    
    NightLabel.textColor = [UIColor yellowColor];
    NightLabel.textAlignment = NSTextAlignmentCenter;
    
    [self.view addSubview:NightLabel];
    

    // Do any additional setup after loading the view.
}
#pragma mark－ 创建Switch点击方法
-(void)NightSwitchAction:(UISwitch *)sender {
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    if (sender.on) {
        //创建通知中心
        [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"night"];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"changColor" object:[UIColor blackColor]];
        
    }else {
        [[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"night"];
        
        //        发送通知
        [[NSNotificationCenter defaultCenter]postNotificationName:@"changColor" object:[UIColor whiteColor]];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
